package com.ty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.entity.AfterSales;
import com.ty.entity.Orders;
import com.ty.mapper.AfterSalesMapper;
import com.ty.mapper.OrdersMapper;
import com.ty.resp.CommonResp;
import com.ty.service.IAfterSalesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 退款表 服务实现类
 * </p>
 *
 * @author txy
 * @since 2024-03-14
 */
@Service
public class AfterSalesServiceImpl extends ServiceImpl<AfterSalesMapper, AfterSales> implements IAfterSalesService {
    @Resource
    private OrdersMapper ordersMapper;

    @Override
    public CommonResp getAfterSales(Page page, String name, String userId, Integer status) {
        CommonResp resp = new CommonResp();
        QueryWrapper<AfterSales> wrapper = new QueryWrapper<>();
        wrapper.apply("a.order_id = oi.order_id and oi.periodical_id = p.id");
        if (status != null) {
            wrapper.eq("a.status", status);
        }
        if (StringUtils.hasText(userId)) {
            wrapper.eq("a.user_id", userId);
        }
        if (StringUtils.hasText(name)) {
            //使用and，将后续的条件括起来，保证条件执行顺序
            wrapper.and(item -> item.like("p.name", name).or().eq("a.order_id", name));
        }
        Page page1 = baseMapper.selectAllAfterSales(page, wrapper);
        resp.setContent(page1.getRecords());
        resp.setTotal(page1.getTotal());
        return resp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResp createAfterSales(AfterSales afterSales) {
        CommonResp resp = new CommonResp();
        if (StringUtils.hasText(afterSales.getUserId()) && StringUtils.hasText(afterSales.getOrderId())) {
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            afterSales.setCreateTime(sdf.format(new Date()));
            resp.setSuccess(baseMapper.insert(afterSales) > 0);
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.set("order_status", 6);
            wrapper.eq("user_id", afterSales.getUserId());
            wrapper.eq("order_id", afterSales.getOrderId());
            ordersMapper.update(wrapper);
        }
        return resp;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CommonResp updateAfterSales(AfterSales afterSales) {
        //修改订单表的状态
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("order_id", afterSales.getOrderId());
        wrapper.eq("user_id", afterSales.getUserId());
        wrapper.set("order_status", afterSales.getStatus());
        int i = ordersMapper.update(wrapper);
        //修改售后表的状态
        UpdateWrapper wrapper1 = new UpdateWrapper();
        wrapper1.eq("order_id", afterSales.getOrderId());
        wrapper1.eq("user_id", afterSales.getUserId());
        wrapper1.set("status ", afterSales.getStatus());
        wrapper1.set("refuse_cause", afterSales.getRefuseCause());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        afterSales.setResultTime(format.format(new Date()));
        wrapper1.set("result_time", afterSales.getResultTime());
        int update = baseMapper.update(wrapper1);
        CommonResp resp = new CommonResp();
        resp.setSuccess(i > 0 && update > 0);
        return resp;
    }
}
